package com.zaoqi.service.impl;

import com.zaoqi.anno.Log;
import com.zaoqi.mapper.LoginMapper;
import com.zaoqi.mapper.UserMapper;
import com.zaoqi.pojo.User;
import com.zaoqi.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
@Log
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return loginMapper.findByUsernameAndPassword(username, password);
    }

    @Override
    public void register(User user) throws Exception {
        // 校验用户名是否已存在
        User existingUser = loginMapper.findByUsername(user.getUsername());
        if (existingUser != null) {
            throw new Exception("用户名已存在");
        }
        // 插入新用户
        userMapper.createUser(user);  // user.id 将自动生成
    }
}
